package com.example.mideaoem.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.util.Log;

public class DateUtil {

	public static int getTimeSheetinMinutes(String endTime, String startTime) {
		int minutes = 0;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d1 = df.parse(endTime);
			Date d2 = df.parse(startTime);
			long diff = d1.getTime() - d2.getTime();
			long minute = diff / (1000 * 60);
			minutes = (int) minute;
		} catch (Exception e) {
		}
		return minutes;
	}

	public static String getTimeStamp(){
		StringBuilder sb = new StringBuilder();
		Date date = new Date(System.currentTimeMillis());
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		sb.append(c.get(Calendar.YEAR));
		sb.append(formatDate(c.get(Calendar.MONTH)));
		sb.append(formatDate(c.get(Calendar.DAY_OF_MONTH)));
		sb.append(formatDate(c.get(Calendar.HOUR_OF_DAY)));
		sb.append(formatDate(c.get(Calendar.MINUTE)));
		sb.append(formatDate(c.get(Calendar.SECOND)));
		
		
		
		return sb.toString();
	}
	
	private static String formatDate(int i){
		if(i < 10){
			return "0"+i;
		}else{
			return ""+i;
		}
	}
	
	/**LOCAL TO UTC 传入/出的格式  09：15 */
	public static String toUTCTime(String time){
		time = enSureFormat(time);
		java.util.Calendar cal = java.util.Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		try{
			Date date = df.parse(time);
			cal.setTime(date);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		
		cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		Date date = cal.getTime();
		return df.format(date);
	}
	
	private static String enSureFormat(String time) {
		try{
		StringBuilder sb = new StringBuilder();
		String[] timeArr = time.split(":");
		if(timeArr[0].length() == 1){
			sb.append("0");
		}
		sb.append(timeArr[0]+":");
		if(timeArr[1].length() == 1){
			sb.append("0");
		}
		sb.append(timeArr[1]);
		return sb.toString();
		}catch(Exception e){
			e.printStackTrace();
			Date d = new Date(System.currentTimeMillis());
			return enSureFormat(d.getHours()+":"+d.getMinutes());
		}
	}

	/**UTC to LOCAL  格式  09：15*/
	public static String toLOCALTime(String time){
		time = enSureFormat(time);
		java.util.Calendar cal = java.util.Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		try{
			Date date = df.parse(time);
			cal.setTime(date);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		
		cal.add(java.util.Calendar.MILLISECOND, +(zoneOffset + dstOffset));
		Date date = cal.getTime();
		return df.format(date);
	}
	
	/** 在当前时间之前 */
	public static boolean isBeforeCurrTime(String timStr){
		Date currentDate = new Date(System.currentTimeMillis());
		Date targetDate = new Date(System.currentTimeMillis());
		try{
			targetDate.setHours(Integer.valueOf(timStr.split(":")[0]));
			targetDate.setMinutes(Integer.valueOf(timStr.split(":")[1]));
		}catch(Exception e){
			e.printStackTrace();
		}
		Log.e("DateUtil","timeStr = "+timStr);
		Log.e("DateUtil","currentDate = "+currentDate.toString());
		Log.e("DateUtil","targetDate = "+targetDate.toString());
		long diff = currentDate.getTime() - targetDate.getTime();
		if(diff <= 0){
			return false;
		}else{
			return true;
		}
	}
	
	/** 1-周日  2-周一*/
	public static int getWeekType(){
		java.util.Calendar cal = java.util.Calendar.getInstance();
		Date currDate = new Date(System.currentTimeMillis());
		cal.setTime(currDate);
		return cal.get(Calendar.DAY_OF_WEEK);
	}
	
	public static String getNextMonth(String currDate){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		Calendar cal = Calendar.getInstance();
		try {
			date = df.parse(currDate);
			cal.setTime(date);
			cal.add(Calendar.MONTH, 1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String result = cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-01"; 
		Log.d("DateUtil","getNextMonth "+result);
		return result;
	}
	
	public static int getDayOfMonth(String dateStr) {
		int dayOfMonth = 0;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date date = df.parse(dateStr);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			dayOfMonth=cal.getActualMaximum(Calendar.DATE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dayOfMonth;
	}
	
	
}
